package com.huobao.shangjia_version.activitys;

import java.util.Map;

import org.simple.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.StoreBaseSetting;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Act_Base_Setting extends HuobaoBaseActivity implements OnClickListener{
	private TextView tv_storename;
	private TextView tv_address;
	private TextView tv_baoyou_jine;
	private LinearLayout llyt_store_jianjie;
	private LinearLayout llyt_nicheng;
	private TextView tv_username;
	private LinearLayout llyt_bangding;
	private TextView tv_bind_shouji;
	private LinearLayout llyt_wangwang;
	private TextView tv_wangwang;
	private LinearLayout llyt_qq;
	private TextView tv_qq;
	
	@ViewInject(R.id.imageview)
	private ImageView imageview;
	@ViewInject(R.id.tv_baoyou_shuliang)
	private TextView tv_baoyou_shuliang;
	@ViewInject(R.id.tv_fanwei)
	private TextView tv_fanwei;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_base_setting;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return getString(R.string.title_2);
	}

	@Override
	protected void init() {
		initView();
		getData();
	}

	private void initView() {
		tv_storename = (TextView) findViewById(R.id.tv_storename);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_baoyou_jine = (TextView) findViewById(R.id.tv_baoyou_jine);
		llyt_store_jianjie = (LinearLayout) findViewById(R.id.llyt_store_jianjie);
		llyt_nicheng = (LinearLayout) findViewById(R.id.llyt_nicheng);
		tv_username = (TextView) findViewById(R.id.tv_username);
		llyt_bangding = (LinearLayout) findViewById(R.id.llyt_bangding);
		tv_bind_shouji = (TextView) findViewById(R.id.tv_bind_shouji);
		llyt_wangwang = (LinearLayout) findViewById(R.id.llyt_wangwang);
		tv_wangwang = (TextView) findViewById(R.id.tv_wangwang);
		llyt_qq = (LinearLayout) findViewById(R.id.llyt_qq);
		tv_qq = (TextView) findViewById(R.id.tv_qq);
		
		llyt_store_jianjie.setOnClickListener(this);
		llyt_nicheng.setOnClickListener(this);
		llyt_bangding.setOnClickListener(this);
		llyt_wangwang.setOnClickListener(this);
		llyt_qq.setOnClickListener(this);
	}
	protected void show(StoreBaseSetting baseSetting) {
		tv_storename.setText(baseSetting.store_name);
		tv_fanwei.setText(baseSetting.send_address);
		tv_address.setText(baseSetting.region_name+" "+baseSetting.address);
		tv_baoyou_jine.setText(baseSetting.acount_for_free_fee);
		tv_baoyou_shuliang.setText(baseSetting.amount_for_free_fee);
		tv_bind_shouji.setText(baseSetting.tel);
		tv_wangwang.setText(baseSetting.im_ww);
		ImageUtils.loadimg(imageview, getIntent().getStringExtra("img"));
	}

	public void onLoginoutClick(View v) {
		SessionUtils.loginout(context);
		EventBus.getDefault().post("", "goFristPage");
		Intent intent = new Intent(this, Act_Login.class);
		startActivity(intent);
		finish();
	}
	private StoreBaseSetting baseSetting;
	private void getData(){
		Map<String, String> map = HttpUtil.getParams("app","shopmember","act","store_shezhi");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				JSONObject object = jsonObject.getJSONObject("member");
				baseSetting = JSON.parseObject(jsonObject.getString("store"), StoreBaseSetting.class);
				String user_name = object.getString("user_name");
				String im_qq = object.getString("im_qq");
				tv_username.setText(user_name);
				tv_qq.setText(im_qq);
				show(baseSetting);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, Act_Store_Seting.class);
		intent.putExtra("data", baseSetting);
		intent.putExtra("qq", tv_qq.getText().toString());
		startActivityForResult(intent, 1);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg0 == 1 && arg1 == RESULT_OK){
			getData();
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
}
